//--------------------------------------------------------------------//
// Do-file: 02_Run_Randomization_Inference 
//
// Paper: "Little evidence that military policing reduces crime 
// or improves human security"
//
// Authors: Robert Blair and Michael Weintraub
//
// Date created: 2022-06-30
// Date last modified:2022-06-30
// 
// Notes:
// (1) This code replicates the randomization of blocks into treatment or control
// status. This is used to apply the randomization inference method. In total the
// code does 100,000 simulations of the randomization process. 
// 
//--------------------------------------------------------------------//

				//////////////////////////////////////
				///								   ///
				///		Randomization Inference	   ///
				///								   ///
				//////////////////////////////////////
				
//---------//
// -- Preliminaries 
//---------//

clear all
set more off
graph set window fontface "Times New Roman"


* Defining main computer path
global path "~/Dropbox/Asistencia de investigacion/Proyecto Cali/Mapas Cali/08_Data&Analysis/02_dofiles/v20220630_PaperReplication" 

		
* Set working directory
cd "${path}" 

	
* Uploading housekeeping
include "${path}/Code/01_Housekeeping.do"
	
		
* Increase max number of variables 
set maxvar 20000
	


			//------------------------------------------------------//
			//- Simulate the randomization procedure 100,000 times -//
			//------------------------------------------------------//

// Note: the following code runs 100,000 simulations of the randomization process
// and creates databases at the block-level that contain treatment variables 
// identifying the treatment status of each block in each simulation. They are 
// organized in batches of 10,000 simulations per database. Thus, 10 databases 
// are created, each with 10,000 treatment variables corresponding to different 
// simulations run within each group.  
					
//------//
//- Randomization 1 - 10,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p1.dta"
	
	
	* Set number of simulations
	local nsimul 10000
	
	
	* Run simulations 
	
	forvalues n = 1/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
		
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p1.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p1.dta", replace
	

	}
	

	
//------//
//- Randomization 10,001 - 20,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p2.dta"
	
	
	* Set number of simulations
	local nsimul 20000
	
	
	* Run simulations 
	
	forvalues n = 10001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
	
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p2.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p2.dta", replace
	

	}


//------//
//- Randomization 20,001 - 30,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p3.dta"
	
	
	* Set number of simulations
	local nsimul 30000
	
	
	* Run simulations 
	
	forvalues n = 20001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
		
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p3.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p3.dta", replace
	

	}
	
	
	
//------//
//- Randomization 30,001 - 40,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p4.dta" 
	
	
	* Set number of simulations
	local nsimul 40000
	
	
	* Run simulations 
	
	forvalues n = 30001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
		
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p4.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p4.dta", replace
	

	}

	

//------//
//- Randomization 40,001 - 50,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p5.dta"
	
	
	* Set number of simulations
	local nsimul 50000
	
	
	* Run simulations 
	
	forvalues n = 40001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
		
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p5.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p5.dta", replace
	

	}
		

		
//------//
//- Randomization 50,001 - 60,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p6.dta"
	
	
	* Set number of simulations
	local nsimul 60000
	
	
	* Run simulations 
	
	forvalues n = 50001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
		
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p6.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p6.dta", replace
	

	}
		
		

//------//
//- Randomization 60,001 - 70,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p7.dta"
	
	
	* Set number of simulations
	local nsimul 70000
	
	
	* Run simulations 
	
	forvalues n = 60001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
	
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p7.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p7.dta", replace
	

	}
		

		
//------//
//- Randomization 70,001 - 80,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p8.dta"
	
	
	* Set number of simulations
	local nsimul 80000
	
	
	* Run simulations 
	
	forvalues n = 70001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
	
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p8.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p8.dta", replace
	

	}
		

		
		
//------//
//- Randomization 80,001 - 90,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p9.dta"
	
	
	* Set number of simulations
	local nsimul 90000
	
	
	* Run simulations 
	
	forvalues n = 80001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
	
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p9.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p9.dta", replace
	

	}
		

		
//------//
//- Randomization 90,001 - 100,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p10.dta"
	
	
	* Set number of simulations
	local nsimul 100000
	
	
	* Run simulations 
	
	forvalues n = 90001/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
	
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p10.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p10.dta", replace
	

	}
		
		
		
	//------//
//- Randomization 90,001 - 100,000 times 
//------//
	
	* Load data 
	use "$data/raw/manzanas_restricted.dta", clear
	
	
	* Save code of the block 
	keep manzana
	save "$data/rand_inf/block_simulate_randomizations_p999.dta"
	
	
	* Set number of simulations
	local nsimul 30
	
	
	* Run simulations 
	
	forvalues n = 1/`nsimul'{
		
		use "$data/raw/manzanas_restricted.dta", clear
	
		di "This is simulation number `n'"
		
		set seed `n'
		
		//- Generate assignment to treatment 1 and treatment 2
	
			* Randomize treatment assignment, stratifying by comuna
			   sort barrio manzana
			   gen rand = runiform()
				sort barrio rand
					bys barrio: gen order = _n 
				
			* Generate treatment dummies
				gen treatment_1 = 0
				gen treatment_2 = 0
				
			* Assign roughly x of all manzanas in each barrio to each of the two treatment groups 
					* Here x = 1/6, which is set dividing number of blocks/12 (see note (2) 
					* in the previous note for an explanation).

				bys barrio_num: gen num_manzanas=_N
				
				gen num_manzanas_t1 = round(num_manzanas/12)
				gen num_manzanas_t2 = num_manzanas_t1*2
					
				levelsof barrio_num, local(barrio_o)
				foreach m of local barrio_o {
					replace treatment_1 = 1 if barrio_num == `m' & order<=num_manzanas_t1
					replace treatment_2 = 1 if barrio_num == `m' & order>num_manzanas_t1 & order<=num_manzanas_t2
					}
				drop num_manzanas_t1 num_manzanas_t2
				
			* Generate treatment and control dummies
				gen control = (treatment_1==0 & treatment_2==0)
				gen treatment = (control==0)		
		
	
		//- Identify spillover blocks 
		
			* Generate variables with the code of each treatment block 
			qui tab manzana if treatment==1, gen(treatment_ID_)
			
			foreach x of varlist treatment_ID_* {
				qui tostring `x', replace
				labnoeq `x'
				}

			foreach x of varlist treatment_ID_* {
				local ID : var label `x'
				qui replace `x' = "`ID'"
				}
			
			* Classify block as spillover if it's within 25meters of a treated block 
			gen potential_spillover = 0
			
			foreach x of varlist manzana_25m* {
				foreach y of varlist treatment_ID_* {
					qui replace potential_spillover = 1 if `x' == `y'
					}
				}
		
		
		//- Define final spillover and control variables
		
			gen spillover = 0
				replace spillover = 1 if potential_spillover==1 & control==1
			
			replace control = 0 if spillover == 1
			
			
			* Define new treatment variable 
			gen treatment_ri = treatment
			
			replace treatment_ri = 2 if spillover == 1
			replace treatment_ri = 0 if control == 1
			
			la def ltreat 0 "Control" 		///
							1 "Treatment"		///
							2 "Spillover"
			la val treatment_ri ltreat
		
			
			* Subset of treatment variables and IDs
			keep treatment_ri manzana
			rename treatment_ri treatment_ri_`n'
			
			
			* Merge with block codes
			merge 1:1 manzana using "$data/rand_inf/block_simulate_randomizations_p999.dta"
				drop _merge
				
			* Save database
			save "$data/rand_inf/block_simulate_randomizations_p999.dta", replace
	

	}
				
				
